package com.smart.manager.product.repository;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Opt;
import com.smart.common.cache.CacheService;
import com.smart.common.entities.constant.ArcoTips;
import com.smart.common.entities.core.Page;
import com.smart.common.entities.option.SelectOption;
import com.smart.manager.product.entities.body.RBasicRfid;
import com.smart.manager.product.entities.enums.RfidStatus;
import com.smart.manager.product.entities.enums.RfidUseStatus;
import com.smart.manager.product.entities.models.BasicRfid;
import com.smart.manager.product.entities.models.query.QBasicRfid;
import io.ebean.DB;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * 游戏管理  耳标数据
 * @author leo
 * @date 2023/09/07
 */
@Service
@Log4j2
@RequiredArgsConstructor
public class BasicRfidRepository {

    private final CacheService cacheService;

    public Page<BasicRfid> pageList(RBasicRfid.SearchBody search) {
        QBasicRfid query = new QBasicRfid();
//        Opt.ofBlankAble(search.getSmartId()).ifPresent(x -> query.smartId.eq(x));
        Opt.ofBlankAble(search.getStatus()).ifPresent(x -> query.status.eq(x));
        Opt.ofBlankAble(search.getUseStatus()).ifPresent(x -> query.useStatus.eq(x));
        Opt.ofBlankAble(search.getBeaconMac()).ifPresent(x -> query.beaconMac.contains(x));
//        query.order().smartId.asc();
        query.setFirstRow(search.firstRow()).setMaxRows(search.maxRows());
        return new Page<BasicRfid>().load(query.findPagedList());
    }


    public void add(RBasicRfid.EditBody body) {
        QBasicRfid query = new QBasicRfid();
        BasicRfid record = query.beaconMac.eq(body.getBeaconMac()).setMaxRows(1).findOne();
        Assert.isNull(record, "创建失败:,蓝牙耳标mac已存在");
        BasicRfid basicRfid = BeanUtil.copyProperties(body, BasicRfid.class);
        basicRfid.setStatus(RfidStatus.AWAIT).setUseStatus(RfidUseStatus.UNUSED).insert();
    }


    public void update(Long id, RBasicRfid.EditBody body) {
        QBasicRfid query = new QBasicRfid();
        BasicRfid record = query.id.eq(id).setMaxRows(1).findOne();
        Assert.notNull(record, ArcoTips.UPDATE_NOT_FOUND);
        BeanUtil.copyProperties(body, record);
        Objects.requireNonNull(record).update();
    }

    public int delByIds(List<Long> ids) {
        List<BasicRfid> list = new QBasicRfid().id.in(ids).findList();
        Assert.notEmpty(list, ArcoTips.DELETE_NOT_FOUND);
        for (BasicRfid item : list) {
            log.warn("判断数据是否能被删除");
        }
        return DB.deleteAll(list);


    }
    

    /**
     * 获取数据字典下拉选项
     * @return {@code List<SelectOption>}
     */
    public List<SelectOption> getOptions() {
        QBasicRfid Rfid = new QBasicRfid();
        return Rfid.findStream().map(x -> new SelectOption().setLabel(x.getName()).setValue(x.getName()).setDisabled(false)).collect(Collectors.toList());
    }
    /**
     * 获取数据字典下拉选项
     * @return {@code List<SelectOption>}
     */
    public List<SelectOption> getRfidOptions() {
        Set<String> name = cacheService.getRfidCache().get("name");
        return name.stream().map(x -> new SelectOption().setLabel(x).setValue(x).setDisabled(false)).collect(Collectors.toList());
    }

}






